<?php
/**
 * @since      File available since Release v1.1
 */

class uploadControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
        Tpl::output('index_sign','index');
        
	}	

	public function addauctionOp(){
		$pdata = $_POST['pdata'];
		$auction_img1 = '';
		$auction_img_all = '';
		
		if(!empty($pdata)){
			$model = Model();
			if(!empty($_POST['auction_id'])){
				$auction_id = intval($_POST['auction_id']);
			}else{
	
				$label_array = $model->table('setting')->where(array('name'=>'auction_label'))->find();
	
				$params = array();
				$params['seller_id'] = $_COOKIE['user_id'];
				$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
				$params['seller_name'] = $user['nickname'];
				$params['auction_sn'] = $label_array['value'].date("YmdHis");
				$params['auction_add_date'] = date("Ymd");				$params['Relation_zc_id']=$_GET["zc_id"];
				$auction_id = $model->table('auction')->insert($params,true);
			}
	
				
				
			$sdata = explode(',', $pdata);
			foreach($sdata as $k=>$val){
				$filename = $this->downloadpic($val);
				$data = array();
				$data['auction_id'] = $auction_id;
				$data['auction_img'] = $filename;
				$model->table('auction_img')->insert($data);
			}
				
				
			echo json_encode(array('state'=>1,'auction_id'=>$auction_id));
			exit;
		}
	}	public function upload_checkOp(){		$model = Model();		$zc_id=$_POST["pdata"];		$label_array = $model->table('special')->where(array('id'=>$zc_id))->find();		$zc_start_time=$label_array['start_time'];		$zc_end_time=$label_array['end_time'];echo json_encode(array('start_time'=>$zc_start_time,'end_time'=>$zc_end_time,'current_time'=>time()));	}
	
	public function downloadpic($mediaid){
		$access_token = $this->getAccessTokenIn();
		$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$mediaid}";
		$fileInfo = $this->downloadWeixinFile($url);
		$array = json_decode($fileInfo["body"],true);
		if(!empty($array['errcode'])){
			$access_token = $this->getAccessToken();
			$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$mediaid}";
			$fileInfo = $this->downloadWeixinFile($url);
		}
	
		$date = date("Ymd");
		$path = BASE_DATA_PATH.'/upload/shop/auction/'.$date;
	
		if(!is_dir($path)){
			mkdir($path,0777,true);
		}
		$filename = "./data/upload/shop/auction/".$date."/{$mediaid}.jpg";
		$this->saveWeixinFile($filename, $fileInfo["body"]);
		$sourcefile = BASE_UPLOAD_PATH.'/shop/auction/'.$date."/{$mediaid}.jpg";
		$desfile_1 = BASE_UPLOAD_PATH.'/shop/auction/'.$date."/{$mediaid}.80_80.jpg";
		$desfile_2 = BASE_UPLOAD_PATH.'/shop/auction/'.$date."/{$mediaid}.640_710.jpg";
		my_image_resize($sourcefile,$desfile_1,80,80);
		my_image_resize($sourcefile,$desfile_2,640,710);
		return "{$mediaid}.jpg";
	}
	
	private function downloadWeixinFile($url) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_NOBODY, 0); //只取body头
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$package = curl_exec($ch);
		$httpinfo = curl_getinfo($ch);
		curl_close($ch);
		$imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
		return $imageAll;
	}
	
	private function saveWeixinFile($filename, $filecontent) {
		$local_file = fopen($filename, 'w');
		if (false !== $local_file){
			if (false !== fwrite($local_file, $filecontent)) {
				fclose($local_file);
			}
		}
	}
}